/**
 * 通用 service
 * [ findAndCountAll, count, create, findById, findOne, update, upsert, destroy, sum ]
 * @param {Object} self service class 对应 this
 */
export default function (self) {
  const { model, args } = self
  // ============================== service 默认方法

  return {
    findAndCountAll: (params, currentPage, pageRow, order) => {
      return model.findAndCountAll({
        where: params,
        offset: (currentPage - 1) * pageRow,
        limit: pageRow,
        // order: order || [['created_at', 'DESC']],
        ...args
      })
    },
    findAll: (params, order) => {
      return model.findAll({
        where: params,
        // order: order || [['created_at', 'DESC']],
        ...args
      })
    },
    count: params => model.count({ where: params }),
    create: params => model.create(params),
    findById: id => model.findById(id, { ...args }),
    findOne: params => model.findOne({ where: params, ...args }),
    update: (params, options) => model.update(params, { where: options }),
    upsert: params => model.upsert(params),
    destroy: params => model.destroy({ where: params }),
    sum: (key, params) => model.sum(key, { where: params })
  }
}
